import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../components/login/Login')
  },
  {
    path: '/home',
    name: 'Home',
    redirect:'/welcome', //重定向 
    component: () => import('../components/Home'),
    children: [
      // 默认展示的
      {
        path: '/welcome',
        name: 'Welcome',
        component: () => import('../components/welcome/Welcome'),
      },
      // 用户列表
      {
        path: '/users',
        name: 'Users',
        component: () => import('../components/user/Users'),
      },
      // 角色权限
      {
        path: '/rights',
        name: 'Rights',
        component: () => import('../components/power/Rights'),
      },
      // 角色列表
      {
        path: '/roles',
        name: 'roles',
        component: () => import('../components/power/Roles'),
      },
      // 商品管理
      {
        path: '/categories',
        name: 'Cate',
        component: () => import('../components/goods/cate/Cate'),
      },
      // 分类参数
      {
        path: '/params',
        name: 'Params',
        component: () => import('../components/goods/params/Params'),
      },
      // 商品列表
      {
        path: '/goods',
        name: 'List',
        component: () => import('../components/goods/list/List'),
      },
      // 添加商品
      {
        path: '/goods/list/add',
        name: 'add',
        component: () => import('../components/goods/list/add'),
      },
      // 
      {
        path: '/orders',
        name: 'Orders',
        component: () => import('../components/order/Order'),
      },
      // 报表组件
      {
        path: '/reports',
        name: 'Reports',
        component: () => import('../components/report/Report.vue'),
      },
    ]
  },
  
]

const router = new VueRouter({
  routes
})

/**
 * 挂载路由守卫
 * to 将要访问的路径
 * from 代表从哪个路径跳转而来
 * next 是一个函数，表示放行
 * next() 放行  next('/login') 强制跳转
 */
router.beforeEach((to, from, next) => {
  // 如果访问的路径是登录页，直接跳转
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
